﻿using System;
using System.IO;

namespace ManagedTFRecordLibrary
{
    public static class TFRecordWriterExtensions
    {

        public static void Write(this TFRecordWriter writer, Example example)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (example == null)
            {
                throw new ArgumentNullException(nameof(example));
            }

            ArraySegment<byte> arr;
            using (var ms = new MemoryStream(81920))
            {
                ProtoBuf.Serializer.Serialize(ms, ExampleConverter.ConvertToTFExample(example));
                if (!ms.TryGetBuffer(out arr))
                {
                    arr = new ArraySegment<byte>(ms.ToArray());
                }
            }

            writer.Write(arr.Array, arr.Offset, arr.Count);
        }
    }
}
